#include "HXSY.h"


byte HXSY::HXNBGET()
{
	char fla = 0;
	while(fla == 0)
	{
		while(ReadOneByte() != 0xAA);//先读一个
		      if(ReadOneByte() == 0xAA)//读第二个
		      {
					   
		        payloadLength = ReadOneByte();//读取第三个，数据包字节的长度
		        if(payloadLength == 0x20)//如果接收到的是大包数据才继续读取，小包数据则舍弃不读取
		        {
		          generatedChecksum = 0; //校验变量清0       
		          for(int i = 0; i < payloadLength; i++)//连续读取32个字节
		          {  
		            payloadData[i] = ReadOneByte();//读取指定长度数据包中的数据
		            generatedChecksum += payloadData[i];//计算数据累加和
		          }         
		          checksum = ReadOneByte();//读取校验字节  
		          //校验
		          generatedChecksum = (~generatedChecksum)&0xff;       
		          //比较校验字节
		          if(checksum == generatedChecksum)//数据接收正确，继续处理 
		          {    
		            signalquality = 0;//信号质量变量
		            attention = 0;    //注意力值变量
		            //赋值数据
		            signalquality = payloadData[1];//信号值      
		            attention = payloadData[29];//注意力值
		            meditation = payloadData[31];//放松度值
					
		            fla = 1;
		            //打印信号质量
		            Serial.print("  SignalQuality: ");
		            Serial.print(signalquality, DEC);
		            //打印注意力值
		            Serial.print("  Attation: ");
		            Serial.print(attention, DEC);
		            //打印放松度值
		            Serial.print("  Meditation: ");
		            Serial.print(meditation, DEC);
		            //换行
		            Serial.print("\n");       
		           
		          } 
				  else
				  {
					  fla = 0;
					  
				  }
		        } 
		      }
	}
	
}
byte HXSY::HXGETTGAM(char sw)
{
    if(sw == 1)
	{
	  return  attention;
		
	}
	else if(sw == 2)
	{
	  return  meditation;
		
	}
   
	
	
}

unsigned char HXSY::HXGETDISTENCE(unsigned char d)
{
	unsigned char buff ;
	unsigned int cnt;
	cs100Abuff[0] = 0XAF;
	cs100Abuff[1] = 0x00;
	cs100Abuff[2] = 0x00;
	cs100Abuff[3] = 0x00;
	
	noInterrupts();
	CS100A_SEND(d);
	
	  pinMode(d,INPUT);
	  delayMicroseconds(10) ;
	  cnt = 0;
	  while(digitalRead(d) == 1)
	  {
		  delayMicroseconds(1);
		  cnt++;
		  if(cnt > 10000)
		  {
			return 300;   
		  }
		  
	  }
	    delayMicroseconds(30) ;//延时100us，开始传递一个字节
	    for(char i=0;i<8;i++)
	    {
	        buff = buff<<1;
	        if (digitalRead(d) == 1)//总线如果被拉高,该位设为1
	        {
	            buff |= 1;   
	        }  
	        delayMicroseconds(100);//等待下一位的信号
	    }
		interrupts();
	  delay(5);
	 
	  return buff;
	
}
HXSY::HXsetColor(unsigned char d,unsigned char s,long int color)
{
   unsigned char buff1;

	if(s == 0)
	{
		cs100Abuff[0] = 0XA3;	
	}
	else if(s == 1)
	{
		cs100Abuff[0] = 0XAC;	
	}
	
	
	cs100Abuff[1] = color & 0xFF;
	cs100Abuff[2] = (color >> 8)&0xFF;
	cs100Abuff[3] = (color >>16)&0xFF;
	
	CS100A_SEND(d);
	delay(10) ;
}
bool HXSY::LIGHT_B_W(unsigned char aa,unsigned char bb,unsigned char cc)
{
	char buff = 0;
	
	  pinMode(aa, OUTPUT);
	  digitalWrite(aa, LOW);//拉低总线，开始通信
	  pinMode(aa, INPUT);
	  delayMicroseconds (130) ;//延时110us
	  if (digitalRead(aa) == 1)//总线如果被拉高，那么开始通信
	  {
	    delayMicroseconds (50) ;//延时100us，开始传递一个字节
	    for(char i=0;i<8;i++)
	    {
	        buff = buff<<1;
	        if (digitalRead(aa) == 1)//总线如果被拉高,该位设为1
	        {
	            buff |= 1;   
	        }  
	        delayMicroseconds (100) ;//等待下一位的信号
	    }
	  }
	  if(cc == 1)//黑
	  {
			if(bb == 0)//左
            {
				if((buff & 0x01) == 0x01)	 return 1;
				else  return 0;
			}
			if(bb == 1)//右
			{
				if((buff & 0x02) == 0x02)	 return 1;
				else  return 0;
			} 
	  }
	  if(cc == 0)//白
	  {
	  			if(bb == 0)//左
	            {
	  				if((buff & 0x01) == 0x00)	 return 1;
	  				else  return 0;
	  			}
	  			if(bb == 1)//右
	  			{
	  				if((buff & 0x02) == 0x00)	 return 1;
	  				else  return 0;
	  			} 
	  }
	
}
bool HXSY::LIGHT5_B_W(unsigned char aa,unsigned char bb,unsigned char cc)
{
	char buff = 0;
	
	 // noInterrupts();
	  
	  
	  pinMode(aa, OUTPUT);
	  digitalWrite(aa, LOW);//拉低总线，开始通信
	  pinMode(aa, INPUT);
	  delayMicroseconds (130) ;//延时110us
	  if (digitalRead(aa) == 1)//总线如果被拉高，那么开始通信
	  {
	    delayMicroseconds (50) ;//延时100us，开始传递一个字节
	    for(char i=0;i<8;i++)
	    {
	        buff = buff<<1;
	        if (digitalRead(aa) == 1)//总线如果被拉高,该位设为1
	        {
	            buff |= 1;   
	        }  
	        delayMicroseconds (100) ;//等待下一位的信号
	    }
	  }
	  if(cc == 1)//黑
	  {
			if(bb == 1)//1
            {
				if((buff & 0x01) == 0x01)	 return 1;
				else  return 0;
			}
			else if(bb == 2)
			{
				if((buff & 0x02) == 0x02)	 return 1;
				else  return 0;	
			}
			else if(bb == 3)
			{
				if((buff & 0x04) == 0x04)	 return 1;
				else  return 0;	
			}
			else if(bb == 4)
			{
				if((buff & 0x08) == 0x08)	 return 1;
				else  return 0;	
			}
			else if(bb == 5)
			{
				if((buff & 0x10) == 0x10)	 return 1;
				else  return 0;	
			}
	  }
	  if(cc == 0)//白
	  {
	  		if(bb == 1)//1
	         {
	  		     if((buff & 0x01) == 0x00)	 return 1;
	  			 else  return 0;
	  		 }
			 else if(bb == 2)
	         {
	  		     if((buff & 0x02) == 0x00)	 return 1;
	  			 else  return 0;
	  		 }
			 else if(bb == 3)
			 {
			     if((buff & 0x04) == 0x00)	 return 1;
			 	 else  return 0;
			 }
			 else if(bb == 4)
			 {
			     if((buff & 0x08) == 0x00)	 return 1;
			 	 else  return 0;
			 }
			 else if(bb == 5)
			 {
			     if((buff & 0x10) == 0x00)	 return 1;
			 	 else  return 0;
			 }
	  }
	 // interrupts();
	
}
bool HXSY::LIGHT5_B_W_1(unsigned char aa,unsigned char bb,unsigned char cc)
{
	char buff = 0;
	
	 // noInterrupts();
	  
	  pinMode(aa, OUTPUT);
	  digitalWrite(aa, LOW);//拉低总线，开始通信
	  pinMode(aa, INPUT);
	  delayMicroseconds (130) ;//延时110us
	  if (digitalRead(aa) == 1)//总线如果被拉高，那么开始通信
	  {
	    delayMicroseconds (50) ;//延时100us，开始传递一个字节
	    for(char i=0;i<8;i++)
	    {
	        buff = buff<<1;
	        if (digitalRead(aa) == 1)//总线如果被拉高,该位设为1
	        {
	            buff |= 1;   
	        }  
	        delayMicroseconds (100) ;//等待下一位的信号
	    }
	  }
	  if(cc == 1)//黑
	  {
			if(bb == 2)
			{
				if((((buff & 0x02) == 0x02) || ((buff & 0x01) == 0x00) || ((buff & 0x10) == 0x00)))	 return 1;
				else  return 0;	
			}
			else if(bb == 4)
			{
				if((((buff & 0x08) == 0x08) || ((buff & 0x01) == 0x00) || ((buff & 0x10) == 0x00)))	 return 1;
				else  return 0;	
			}
	  }
	  if(cc == 0)//白
	  {
	  		 if(bb == 2)
	         {
	  		     if(((buff & 0x02) == 0x00) || ((buff & 0x01) == 0x01) || ((buff & 0x10) == 0x10))	 return 1;
	  			 else  return 0;
	  		 }
			 else if(bb == 4)
			 {
			     if(((buff & 0x08) == 0x00) || ((buff & 0x01) == 0x01) || ((buff & 0x10) == 0x10))	 return 1;
			 	 else  return 0;
			 }
	  }
	 // interrupts();
	
}
HXSY::WriteSEG(unsigned char KEY2,unsigned int SEGdat)
{
	SEG[0] = SEGdat/1000%10;
	SEG[1] = SEGdat/100%10;
	SEG[2] = SEGdat/10%10;
	SEG[3] = SEGdat%10;
	
	pinMode(KEY2, OUTPUT);
	
	  for(char i=0;i<4;i++)
	  {
	       digitalWrite(KEY2, LOW);//拉低总线，请求通信
	       delayMicroseconds (100) ;
	       digitalWrite(KEY2, HIGH);//拉低总线，请求通信
	       segbuff = SEG[i];
	        for(char j=0;j<8;j++)
	        {
	            if(segbuff & 0x80)
	            {
	                digitalWrite(KEY2, HIGH); 
	                delayMicroseconds (100) ;//延时100us
	            }
	            else
	            {
	                digitalWrite(KEY2, LOW); 
	                delayMicroseconds (100) ;//延时100us
	            }
	            segbuff = segbuff<<1; 
	        }     
	  }
	delayMicroseconds (500) ;
	
}
HXSY::waitbutton(unsigned char KEY1,unsigned char swKEY1)
{
	while(buttonPressed(KEY1,swKEY1)  == 1)	
	{
		delayMicroseconds (100) ;		
	}
	
}
bool HXSY::buttonPressed(unsigned char KEY,unsigned char swKEY)
{
	  char buff = 0;
	
	  pinMode(KEY, OUTPUT);
	  digitalWrite(KEY, LOW);//拉低总线，开始通信
	  pinMode(KEY, INPUT);
	  delayMicroseconds (130) ;//延时110us
	  if (digitalRead(KEY) == 1)//总线如果被拉高，那么开始通信
	  {
	    delayMicroseconds (50) ;//延时100us，开始传递一个字节
	    for(char i=0;i<8;i++)
	    {
	        buff = buff<<1;
	        if (digitalRead(KEY) == 1)//总线如果被拉高,该位设为1
	        {
	            buff |= 1;   
	        }  
	        delayMicroseconds (100) ;//等待下一位的信号
	    }
	  }
        if(swKEY == 1)
		{
			if((buff & 0XAF)== 0xa7) 
			 {  return 1;}
			else
			{   return 0;}
	    }
		if(swKEY == 2)
		{
			if((buff & 0XAF)== 0xaB) 
			 {  return 1;}
			else
			{   return 0;}
		}
		if(swKEY == 3)
		{
			if((buff & 0XAF)== 0xaD) 
			 {  return 1;}
			else
			{   return 0;}
		}
		if(swKEY == 4)
		{
			if((buff & 0XAF)== 0xaE) 
			 {  return 1;}
			else
			{   return 0;}
		}
			

	
}

HXSY::MOTOR_STOP_ONE(unsigned char aaa)
{
     MOTORcorl[aaa] = 0;
	 MOTOR_send();
}

HXSY::MOTOR_SEND(unsigned char aa,int bb)
{
	unsigned char buff;
  
	if(bb>0)
	{
		buff = 	0X80 | bb;		
	}
	else if(bb <0)
	{
		buff = 0X40 | ((-1)*bb);
	}
	else if(bb == 0)
	{
	    buff = 0;
	}
	MOTORcorlbuff[aa] = buff;
	/*
	if(MOTORcorlbuff[aa] != MOTORcorl[aa])
	{
		MOTORcorl[aa]	= MOTORcorlbuff[aa];		
	}
    */
}
HXSY::MOTOR_START()
{
	if((MOTORcorlbuff[0] != MOTORcorl[0]) || (MOTORcorlbuff[1] != MOTORcorl[1]) || (MOTORcorlbuff[2] != MOTORcorl[2]) || (MOTORcorlbuff[3] != MOTORcorl[3])  )
	{
		
	   MOTORcorl[0] = MOTORcorlbuff[0];
	   MOTORcorl[1] = MOTORcorlbuff[1];
	   MOTORcorl[2] = MOTORcorlbuff[2];
	   MOTORcorl[3] = MOTORcorlbuff[3];
	   MOTOR_send();

	 }
}
HXSY::MOTOR_STOP()
{
	
	MOTORcorl[0] = 0;
	MOTORcorl[1] = 0;	
	MOTORcorl[2] = 0;	
	MOTORcorl[3] = 0;	
	MOTOR_send();
}
HXSY::MOTOR_SEND_START(unsigned char aa,int bb)
{
		MOTOR_SEND(aa,bb);
	    MOTOR_START();
}
HXSY::DJset(unsigned char rg6,unsigned char rg7,unsigned char rg8,unsigned char rg9)
{
	int r6,r7,r8,r9;
	
	r6 = rg6 * 11 + 500;
	r7 = rg7 * 11 + 500;
	r8 = rg8 * 11 + 500;
	r9 = rg9 * 11 + 500;	
	
	pinMode(6, OUTPUT);
	pinMode(7, OUTPUT);
	pinMode(8, OUTPUT);
	pinMode(9, OUTPUT);
	 
	 Serial.println(r6);
	 
	for (int i = 1; i <= 120; i++) {
		
	  digitalWrite(6, HIGH);
	  delayMicroseconds(r6);
	  digitalWrite(6, LOW);
	  delayMicroseconds(20000 - r6);
	  
	  digitalWrite(7, HIGH);
	  delayMicroseconds(r7);
	  digitalWrite(7, LOW);
	  delayMicroseconds(20000 - r7);
	  
	  digitalWrite(8, HIGH);
	  delayMicroseconds(r8);
	  digitalWrite(8, LOW);
	  delayMicroseconds(20000 - r8);
	  
	  digitalWrite(9, HIGH);
	  delayMicroseconds(r9);
	  digitalWrite(9, LOW);
	  delayMicroseconds(20000 - r9);
	}

}
HXSY::DJset1(unsigned char sw,unsigned char rg)
{
	int rgg;
	
	noInterrupts();
	
	rgg = rg * 11 + 500;
	for (int i = 1; i <= 200; i++) {
	  pinMode(sw, OUTPUT);
	  digitalWrite(sw, HIGH);
	  delayMicroseconds(rgg);
	  digitalWrite(sw, LOW);
	  delayMicroseconds(20000 - rgg);
	}
	
	interrupts();
}
HXSY::DJset2(unsigned char sw,unsigned char sp,unsigned char rg,unsigned char rg2)
{
	int rgg;
	
	if(rg2>rg)
	{ 
		for(int j = rg;j<=rg2;j++)
		{
			rgg = j * 11 + 500;
			noInterrupts();
						  
			for (int i = 1; i <= 1; i++) {
			  pinMode(sw, OUTPUT);
			  digitalWrite(sw, HIGH);
			  delayMicroseconds(rgg);
			  digitalWrite(sw, LOW);
			  delayMicroseconds(20000 - rgg);
			}
			interrupts();
			
			delay(100-sp);
			
		}
	}
	else if(rg2<rg)
	{
		for(int j = rg;rg2<=j;j--)
		{
			noInterrupts();
			
			rgg = j * 11 + 500;
			for (int i = 1; i <= 1; i++) {
			  pinMode(sw, OUTPUT);
			  digitalWrite(sw, HIGH);
			  delayMicroseconds(rgg);
			  digitalWrite(sw, LOW);
			  delayMicroseconds(20000 - rgg);
			}
			
			interrupts();
			delay(100-sp);
			
		}
	}
		
	
}


/*void hxSetServo(int servopin, int angle) {
  for (int i = 1; i <= 10; i++) {
    volatile int pulsewidth = angle * 11 + 500;
    pinMode(servopin, OUTPUT);
    digitalWrite(servopin, HIGH);
    delayMicroseconds(pulsewidth);
    digitalWrite(servopin, LOW);
    delayMicroseconds(20000 - pulsewidth);
  }
}
*/


